package com.yc.sole.erp.controller;

import com.yc.sole.common.domain.StateCountResp;
import com.yc.sole.erp.biz.ProduceBiz;
import com.yc.sole.erp.request.ProduceQueryReq;
import com.yc.sole.erp.request.ProduceReq;
import com.yc.sole.erp.response.ProduceResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 生产
 *
 * @author yizuomin
 * @date 2024-01-13
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class ProduceController {

    private final ProduceBiz produceBiz;

    /**
     * 新增生产信息
     *
     * @param produceReq
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:save", description = "新增生产信息")
    @PostMapping("/api/erp/produce/saveProduce")
    public Result<Void> saveProduce(@RequestBody @Validated(SaveGroup.class) ProduceReq produceReq) {
        produceBiz.saveProduce(produceReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param produceReq
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:update", description = "修改生产信息")
    @PostMapping("/api/erp/produce/updateProduceById")
    public Result<Void> updateProduceById(@RequestBody @Validated(UpdateGroup.class) ProduceReq produceReq) {
        produceBiz.updateProduceById(produceReq);
        return Result.success();
    }

    /**
     * 删除生产
     *
     * @param baseBatchIdsReq
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:delete", description = "删除生产")
    @PostMapping("/api/erp/produce/deleteProduce")
    public Result<Void> deleteProduce(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseBatchIdsReq) {
        produceBiz.deleteProduce(baseBatchIdsReq.getIds());
        return Result.success();
    }

    /**
     * 暂停生产
     *
     * @param baseBatchIdsReq
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:update", description = "暂停生产")
    @PostMapping("/api/erp/produce/pauseProduce")
    public Result<Void> pauseProduce(@RequestBody @Validated(UpdateGroup.class) BaseBatchIdsReq baseBatchIdsReq) {
        produceBiz.pauseProduce(baseBatchIdsReq.getIds());
        return Result.success();
    }

    /**
     * 恢复生产
     *
     * @param baseBatchIdsReq
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:update", description = "恢复生产")
    @PostMapping("/api/erp/produce/recoveryProduce")
    public Result<Void> recoveryProduce(@RequestBody @Validated(UpdateGroup.class) BaseBatchIdsReq baseBatchIdsReq) {
        produceBiz.recoveryProduce(baseBatchIdsReq.getIds());
        return Result.success();
    }

    /**
     * 通过Id查询生产信息
     *
     * @param id
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:select", description = "通过Id查询生产信息")
    @PostMapping("/api/erp/produce/selectProduceById/{id}")
    public Result<ProduceResp> selectProduceById(@PathVariable Long id) {
        return Result.data(produceBiz.selectProduceById(id));
    }

    /**
     * 通过排产编号查询排产信息
     *
     * @param queryReq
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:select", description = "通过排产编号查询排产信息")
    @PostMapping("/api/erp/produce/selectByProduceNo")
    public Result<List<ProduceResp>> selectByProduceNo(@RequestBody ProduceQueryReq queryReq) {
        return Result.data(produceBiz.selectByProduceNo(queryReq.getProduceNo()));
    }

    /**
     * 通过订单Id查询生产信息
     *
     * @param orderId
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:select", description = "通过订单Id查询生产信息")
    @PostMapping("/api/erp/produce/selectProduceByOrderId/{orderId}")
    public Result<ProduceResp> selectProduceByOrderId(@PathVariable Long orderId) {
        return Result.data(produceBiz.selectProduceByOrderId(orderId));
    }

    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:produce:select", description = "查询生产信息")
    @PostMapping("/api/erp/produce/selectProducePage")
    public Result<Paging<ProduceResp>> selectProducePage(@RequestBody @Validated BasePageReq<ProduceQueryReq> basePageReq) {
        return Result.data(produceBiz.selectProducePage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 查询可以产量登记的生产
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:produce:select", description = "查询可以产量登记的生产")
    @PostMapping("/api/erp/produce/selectProduceRegistrationPage")
    public Result<Paging<ProduceResp>> selectProduceRegistrationPage(@RequestBody @Validated BasePageReq<ProduceQueryReq> basePageReq) {
        return Result.data(produceBiz.selectProduceRegistrationPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 打印生产
     *
     * @param produceQueryReq
     * @return Result
     */
    @PreAuth(value = "erp:produce:select", description = "打印生产")
    @PostMapping("/api/erp/produce/selectProduceRegistrationList")
    public Result<List<ProduceResp>> selectProduceRegistrationList(@RequestBody @Validated ProduceQueryReq produceQueryReq) {
        return Result.data(produceBiz.selectProduceRegistrationList(produceQueryReq));
    }

    /**
     * 生产
     *
     * @param baseBatchIdsReq
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:update", description = "生产")
    @PostMapping("/api/erp/produce/produce")
    public Result<Void> produce(@RequestBody @Validated(UpdateGroup.class) BaseBatchIdsReq baseBatchIdsReq) {
        produceBiz.produce(baseBatchIdsReq.getIds());
        return Result.success();
    }

    /**
     * 查询生产状态的数量
     *
     * @return RestResult
     */
    @PreAuth(value = "erp:produce:select", description = "查询生产状态的数量")
    @PostMapping("/api/erp/produce/selectProduceStateCount")
    public Result<List<StateCountResp>> selectProduceStateCount(@RequestBody ProduceQueryReq queryReq) {
        return Result.data(produceBiz.selectProduceStateCount(queryReq));
    }
}
